<?php

namespace app\traits;

use helper\Elastic as Helper;

/*
 * 数据表缓存 (单条,多条)
 */
trait Elastic
{
    /*
     * 写入elastic数据库
     */
    public static function bulk(){
        $index = self::$elasticIndex;
        $es = new Helper;
        if(!$es->exists($index)){
            $es->create($index, self::$elasticField);
        }
        $size = 20000;
        $count = self::where([])->count('id');
        $l = ceil($count / $size);
        for($p=0; $p < $l; $p++){
            $rows = self::field(array_keys(self::$elasticField))->page($p, $size)->select()->toArray();
            if($es->bulk($index, $rows)){
                return false;
            }
        }
        return true;
    }
}
